package org.oc.qaq.controller;

import org.oc.qaq.context.UserContext;
import org.oc.qaq.dto.PasswordDTO;
import org.oc.qaq.dto.UserDTO;
import org.oc.qaq.result.Result;
import org.oc.qaq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/avatar")
    public Result<?> uploadAvatar(@RequestPart(name = "avatar") MultipartFile avatar) throws IOException {
        return userService.uploadAvatar(UserContext.getCurrentId(), avatar);
    }

    @GetMapping("/me")
    public Result<?> me() {
        return userService.me();
    }

    @PutMapping("/me")
    public Result<?> update(@RequestBody UserDTO user) throws IOException {
        userService.updateMe(user);
        return Result.success();
    }

    @PutMapping("/me/chang-password")
    public Result<?> changePassword(@RequestBody PasswordDTO passwordDTO) {
        userService.changePassword(passwordDTO);
        return Result.success();
    }

}
